package com.cjhme.common.util;

import java.io.File;
import java.util.UUID;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import com.cjhme.common.model.fileupload.FileDataBean;

/**
 * 
 * <p>  
 * Title: UploadImgUtil.java 
 * </p>  
 * Description: 图片上传
 * <p>
 * Modify histoty:
 * @author  cjh  
 * @version 1.0
 */
public class UploadImgUtil {
	
	static Log LOGGER = LogFactory.getLog(UploadImgUtil.class);
	

	/**
	 * 文件上传
	 * @param request request请求
	 * @param formFileName 表单中的file对象名
	 * @param dir   存在目录
	 * @return
	 */
	public static FileDataBean upload(HttpServletRequest request,String formFileName,String dir,boolean isNewFileName){
		FileDataBean fileData = new FileDataBean();
		//转换为MultipartHttpRequest对象
		MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
		
	
		//实例化File对象
		File mdFile = new File(dir);
		
		//如果目录不存在，创建一个目录
		if(!mdFile.exists()){
			mdFile.mkdirs();
		}
		
		//页面控件的文件流
		MultipartFile multipartFile = multipartRequest.getFile(formFileName);
		
		//判断上传的文件是否为空
		if(null!=multipartFile && !multipartFile.isEmpty()){
		
			//判断是否需要重新生成文件名
			String fileName="";
			if(isNewFileName){
				String suffix = multipartFile.getOriginalFilename().substring(multipartFile.getOriginalFilename().lastIndexOf("."));
				fileName =  UUID.randomUUID().toString()+ suffix;
			}else{
				fileName=multipartFile.getOriginalFilename();
			}
			
			//拼接文件路径
			String filePath = dir +File.separator + fileName;
			
			//实例化File对象
			File savaFile  = new File(filePath);
			
		    try {
				//写入文件
				multipartFile.transferTo(savaFile);
				fileData.setFileName(fileName);
				fileData.setFileUrl(filePath);
				
			} catch (Exception e) {
				e.printStackTrace();
				fileData=null;
			}
			
		}
		
		return fileData;
	}
	
	
	/**
	 * 图片上传压缩处理
	 * @param request request请求
	 * @param formFileName 表单中的file对象名
	 * @param dir   存在目录
	 * @param compression 是否压缩
	 * @param width 压缩指定宽度，无压缩时传0
	 * @param height 压缩指定高度，无压缩时传0
	 * @return
	 */
	public static FileDataBean uploadImgCompression(HttpServletRequest request,String formFileName,String dir,boolean compression,boolean isNewFileName,int width,int height){
		FileDataBean fileData = new FileDataBean();
		//转换为MultipartHttpRequest对象
		MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
		
	
		//实例化File对象
		File mdFile = new File(dir);
		
		//如果目录不存在，创建一个目录
		if(!mdFile.exists()){
			mdFile.mkdirs();
		}
		
		//页面控件的文件流
		MultipartFile multipartFile = multipartRequest.getFile(formFileName);
		
		//判断上传的文件是否为空
		if(!multipartFile.isEmpty()){
		
			//判断是否需要重新生成文件名
			String fileName="";
			if(isNewFileName){
				String suffix = multipartFile.getOriginalFilename().substring(multipartFile.getOriginalFilename().lastIndexOf("."));
				fileName =  UUID.randomUUID().toString()+ suffix;
			}else{
				fileName=multipartFile.getOriginalFilename();
			}
			
			//拼接文件路径
			String filePath = dir +File.separator + fileName;
			
		    try {
		    	//判断是否压缩
		    	if(compression){
		    		PhotoCompression.photoAllCompression(multipartFile.getInputStream(), filePath, width,height);
		    	}else{
		    		//实例化File对象
					File savaFile  = new File(filePath);
					//写入文件
					multipartFile.transferTo(savaFile);
		    	}
		    	
				fileData.setFileName(fileName);
				fileData.setFileUrl(filePath);
				
			} catch (Exception e) {
				e.printStackTrace();
				fileData=null;
			}
			
		}
		
		return fileData;
		
	}
	
}
